package com.qingcheng.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.system.Resource;
import com.qingcheng.service.system.ResourceService;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Reference
    private ResourceService resourceService;

    @GetMapping("/findAll")
    public List<Resource> findAll(){
        return resourceService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Resource> findPage(int page, int size){
        return resourceService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Resource> findList(@RequestBody Map<String,Object> searchMap){
        return resourceService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Resource> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  resourceService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Resource findById(Integer id){
        return resourceService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Resource resource){
        resourceService.add(resource);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Resource resource){
        resourceService.update(resource);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Integer id){
        resourceService.delete(id);
        return new Result();
    }

    //////////////////////////////////////////////////////////////////////////////

    /**
     * 查询所有权限,返回list集合形式
     * @return
     */
    @GetMapping("/findAllResources")
    public List<Map> findAllResources() {

        return resourceService.findAllResources();

    }

    /**
     * 保存所设置的权限
     * @param checkedResources
     * @return
     */
    @PostMapping("/saveResources")
    public Result saveResources(Integer roleId, @RequestBody Integer[] checkedResources){
        System.out.println("执行到这一步了");
        System.out.println("checkedResources:"+checkedResources);
        resourceService.saveResources(roleId,checkedResources);
        return new Result();
    }

    /**
     * 根据roleId查询其拥有的角色
     * @param roleId
     * @return
     */
    @GetMapping("/findResourceByRoleId")
    public Integer[] findResourceByRoleId(Integer roleId){
        return resourceService.findResourceByRoleId(roleId);
    }

    @GetMapping("/findResKeyByLoginName")
    public List<String> findResKeyByLoginName(String loginName){
        return resourceService.findResKeyByLoginName(loginName);
    }


}
